package dao;

import entities.UserJdo;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

import java.sql.Date;
import java.util.List;

public class DBUserDAOImpl implements UserDAO{
    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory){
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    public void addUser(UserJdo user) {
        hibernateTemplate.saveOrUpdate(user);
    }

    public void removeUser(UserJdo user) {
        hibernateTemplate.delete(user);
    }

    public void updateUser(Long userId, UserJdo user) {
        hibernateTemplate.saveOrUpdate(user);
    }

    public List<UserJdo> getUserById(Long userId) {
        return  (List<UserJdo>) hibernateTemplate.findByCriteria(DetachedCriteria.forClass(UserJdo.class)
                .add(Restrictions.eq("USER_ID", userId)));
    }

    public List<UserJdo> getUsersByCompanyId(Long companyId) {
        //hibernateTemplate.findByValueBean()
        List list = hibernateTemplate.find("from TM_USER tmu, TM_COMPANY tmu, TM_GROUP tmg " +
                "where tmg.COMPANY_ID = ? and tmg.COMPANY_ID = tmu.COMPANY_ID and tmu.GROUP_ID = tmg.GROUP_ID", companyId);
        return (List<UserJdo>) list;
    }

    public List<UserJdo> getUsersByGroupId(Long groupId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<UserJdo> getUsersByDateRegistered(Date dateRegistered) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<UserJdo> getUsersRegisteredInPeriod(Date startDate, Date endDate) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
